package com.fdd.furns.web;

import com.fdd.furns.entity.Member;
import com.fdd.furns.service.MemberService;
import com.fdd.furns.service.impl.MemberServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/loginServlet")
public class LoginServlet extends HttpServlet {
    MemberService memberService = new MemberServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("loginServlet被调用");
        String username = req.getParameter("username1");
        String password = req.getParameter("password1");
        Member member = new Member(null, username, password, null);
        if (memberService.login(member) != null) {
            //说明登录成功，跳转页面
//           resp.sendRedirect("/furnishing/views/member/login_ok.jsp");
            //将登录成功的用户放入session域中
            req.getSession().setAttribute("username", username);
            req.getRequestDispatcher("/views/member/login_ok.jsp").forward(req, resp);
        } else {
            //登录失败，给出提示信息，不改变用户名
            System.out.println("登录失败");
            req.setAttribute("errorMes", "用户名或密码错误");
            req.setAttribute("username", username);
            req.getRequestDispatcher("/views/member/login.jsp").forward(req, resp);
        }

    }
}
